package com.xindaoapp.happypet.shop.ui;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.widget.DrawerLayout;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.activity.GoodsSerachActivity;
import com.xindaoapp.happypet.activity.MainActivity;
import com.xindaoapp.happypet.bean.RecommendCicleData;
import com.xindaoapp.happypet.fragments.ShoppingCartFragment;
import com.xindaoapp.happypet.ui.BaseFragment;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.view.FixGridLayout;

public class ShopIndexFragment extends BaseFragment implements OnClickListener {

    private static final String[] CONTENT = new String[] {"汪星人", "喵星人"};
    private boolean mIsLoaded;
    private TextView tv_title;
    private ImageView iv_left, iv_right;
    /** 切换tab */
    private ViewPager viewPager;
    private PopupWindow mPopupWindow1, mPopupWindow2;
    private List<RecommendCicleData.RecommendCicle> mCicles1, mCicles2;
    private FixGridLayout layout_changyong, layout_huodong;
    private RelativeLayout lay_scroll;
    private DrawerLayout mDrawerLayout;
    private ImageView iv_closepop, iv_refresh;
    private Animation operatingAnim;

    /**
     * tab初始化
     */
    private View tab_wang, tab_miao;
    private View iv_point1, iv_point2;
    private TextView tv_wang, tv_miao, tv_line1, tv_line2;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return mView = inflater.inflate(R.layout.activity_shop, container, false);
    }


    @Override
    protected void initViews() {
        super.initViews();


        Fragment[] fragments =
                new Fragment[] {ShopFragment.newInstance("汪星人"), ShopFragment.newInstance("喵星人")};

        iv_point1 = mView.findViewById(R.id.iv_point1);
        iv_point2 = mView.findViewById(R.id.iv_point2);

        tv_line1 = (TextView) mView.findViewById(R.id.tv_line1);
        tv_line2 = (TextView) mView.findViewById(R.id.tv_line2);

        tv_wang = (TextView) mView.findViewById(R.id.tv_wang);
        tv_miao = (TextView) mView.findViewById(R.id.tv_miao);
        tab_wang = mView.findViewById(R.id.tab_wang);
        tab_miao = mView.findViewById(R.id.tab_miao);

        viewPager = (ViewPager) mView.findViewById(R.id.pager);


        SpPagerAdapter adapter = new SpPagerAdapter(getChildFragmentManager(), fragments);

        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(2);
        viewPager.setOnPageChangeListener(adapter);

    }


    private void changeTab(int i) {
        iniTabSelectedSetting();
        switch (i) {
            case 0:
                tv_wang.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
                tab_wang.setSelected(true);
                tv_line1.setVisibility(View.VISIBLE);
                goAnim(tv_line1);
                break;
            case 1:
                if (iv_point2.getVisibility() == View.VISIBLE) {
                    iv_point2.setVisibility(View.GONE);
                }
                tv_miao.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
                tab_miao.setSelected(true);
                tv_line2.setVisibility(View.VISIBLE);
                goAnim(tv_line2);
                break;

            default:
                break;
        }

    }


    private void goAnim(TextView tv_line) {
        final AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f, 1.0f);
        alphaAnimation.setDuration(200);
        tv_line.startAnimation(alphaAnimation);
    }

    private void iniTabSelectedSetting() {
        tv_wang.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        tv_miao.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        tab_wang.setSelected(false);
        tab_miao.setSelected(false);
        tv_line1.setVisibility(View.GONE);
        tv_line2.setVisibility(View.GONE);
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        // mView.findViewById(R.id.shouye).setOnClickListener(this);
        // mView.findViewById(R.id.geren).setOnClickListener(this);
        mView.findViewById(R.id.search).setOnClickListener(this);
        mView.findViewById(R.id.buycar).setOnClickListener(this);
        tab_wang.setOnClickListener(this);
        tab_miao.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub
        switch (view.getId()) {

            case R.id.tab_wang:
                viewPager.setCurrentItem(0);
                break;
            case R.id.tab_miao:
                viewPager.setCurrentItem(1);
                break;
            // case R.id.shouye:// 首页
            //
            // break;
            // case R.id.geren:// 个人中心
            // startActivity(new Intent(mContext, GoodsClassActivity.class));
            // break;
            case R.id.search:// 搜索
                Intent sreach = new Intent(mContext, GoodsSerachActivity.class);
                startActivity(sreach);
                break;
            case R.id.buycar:// 购物车
                if (UserState.isLogged(getActivity())) {
                    Intent mainIntent = new Intent(mContext, MainActivity.class);
                    mainIntent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                            ShoppingCartFragment.class.getCanonicalName());
                    startActivity(mainIntent);
                }
                break;
        }

    }


    @SuppressLint("NewApi")
    /**
     * tab切换适配器
     * 
     * @author Stone
     * 
     */
    public class SpPagerAdapter extends FragmentPagerAdapter implements OnPageChangeListener {
        Fragment[] fragments;

        public SpPagerAdapter(FragmentManager fm, Fragment[] fragments) {
            super(fm);
            this.fragments = fragments;
            changeTab(0);
        }

        @Override
        public Fragment getItem(int arg0) {
            return fragments[arg0];
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {}

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {}

        @Override
        public void onPageSelected(int position) {
            changeTab(position);
        }


    }



    //


    // @Override
    // public void onCreate(Bundle savedInstanceState) {
    // super.onCreate(savedInstanceState);
    // }
    //
    // @Override
    // public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
    // savedInstanceState) {
    // return inflater.inflate(R.layout.activity_shop, container, false);
    // }
    //
    // @Override
    // protected void initViews() {
    // // 商城禁用左侧滑动
    // mDrawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
    // // mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    //
    // FragmentPagerAdapter adapter = new GoogleMusicAdapter(getFragmentManager());
    //
    // ViewPager pager = (ViewPager) getView().findViewById(R.id.pager);
    // pager.setAdapter(adapter);
    //
    // // TabPageIndicator indicator = (TabPageIndicator) getView().findViewById(R.id.indicator);
    // // indicator.setViewPager(pager);
    // }
    //
    //
    // @Override
    // protected void initEvents() {
    // super.initEvents();
    // // getView().findViewById(R.id.shouye).setOnClickListener(mOnClickListener);
    // // getView().findViewById(R.id.geren).setOnClickListener(mOnClickListener);
    // getView().findViewById(R.id.search).setOnClickListener(mOnClickListener);
    // getView().findViewById(R.id.buycar).setOnClickListener(mOnClickListener);
    // }
    //
    //
    // class GoogleMusicAdapter extends FragmentPagerAdapter {
    // public GoogleMusicAdapter(FragmentManager fm) {
    // super(fm);
    // }
    //
    // @Override
    // public ShopFragment getItem(int position) {
    // return ShopFragment.newInstance(CONTENT[position % CONTENT.length]);
    // }
    //
    // @Override
    // public CharSequence getPageTitle(int position) {
    // return CONTENT[position % CONTENT.length].toUpperCase();
    // }
    //
    // @Override
    // public int getCount() {
    // return CONTENT.length;
    // }
    // }
    //
    //
    // private final OnClickListener mOnClickListener = new OnClickListener() {
    // @Override
    // public void onClick(View v) {
    // switch (v.getId()) {
    // case R.id.shouye:// 首页
    //
    // break;
    // case R.id.geren:// 个人中心
    // startActivity(new Intent(mContext, GoodsClassActivity.class));
    // break;
    // case R.id.search:// 搜索
    // Intent sreach = new Intent(mContext, GoodsSerachActivity.class);
    // startActivity(sreach);
    // break;
    // case R.id.buycar:// 购物车
    // if (UserState.isLogged(getActivity())) {
    // Intent mainIntent = new Intent(mContext, MainActivity.class);
    // mainIntent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
    // ShoppingCartFragment.class.getCanonicalName());
    // startActivity(mainIntent);
    // }
    // break;
    // }
    //
    // }
    //
    // };
    // private DrawerLayout mDrawerLayout;


}
